package com.xuan.job.service.user.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.xuan.job.domain.user.UserContest;
import com.xuan.job.domain.user.UserScore;
import com.xuan.job.mapper.UserContestMapper;
import com.xuan.job.service.user.UserContestService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author ASUS
 * @description 针对表【tb_user_contest(用户-竞赛 关系表)】的数据库操作Service实现
 * @createDate 2024-11-26 22:28:59
 */
@Service
@Slf4j
public class UserContestServiceImpl extends ServiceImpl<UserContestMapper, UserContest>
        implements UserContestService {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    public void batchUpdateUserScoreAndRank(List<UserScore> userScoreList) {
        try (SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH)) {
            UserContestMapper userContestMapper = session.getMapper(UserContestMapper.class);
            for (UserScore userScore : userScoreList) {
                userContestMapper.updateUserScoreAndRank(userScore);
            }
            session.commit();
        } catch (Exception e) {
            log.error("批量更新用户竞赛信息失败,{}", e.getMessage());
            throw new RuntimeException();
        }
    }

}




